package com.newhero.product.common.shiro;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import javax.annotation.Resource;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.cas.CasRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

import com.newhero.product.module.sys.model.Organ;
import com.newhero.product.module.sys.model.User;
import com.newhero.product.module.sys.model.vo.UserVo;
import com.newhero.product.module.sys.service.IAppService;
import com.newhero.product.module.sys.service.IOrganService;
import com.newhero.product.module.sys.service.IRoleService;
import com.newhero.product.module.sys.service.IUserService;

/**
 * 用户授权信息域
 * 
 * @author coderhuang
 * 
 */
public class UserRealm extends CasRealm {

	private static final Logger LOGGER = LogManager.getLogger(UserRealm.class);

	@Autowired
	private IUserService userService;
	@Autowired
	private IRoleService roleService;
	@Autowired
	private IAppService appServiceImpl;
	@Autowired
	private IOrganService organServiceImpl;

	protected final Map<String, SimpleAuthorizationInfo> roles = new ConcurrentHashMap<String, SimpleAuthorizationInfo>();

	/**
	 * 1、CAS认证 ,验证用户身份 2、将用户基本信息设置到会话中
	 */
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) {

		LOGGER.info("Shiro开始登录认证");

		AuthenticationInfo authc = super.doGetAuthenticationInfo(token);

		String userName = (String) authc.getPrincipals().getPrimaryPrincipal();
		System.out.println(userName);
		// User user = userService.getUserByAccount(account);
		//
		// SecurityUtils.getSubject().getSession().setAttribute("user", user);
		UserVo uservo = new UserVo();
		uservo.setUserLoginName(userName);
		List<User> list = userService.selectByLoginName(uservo);
		// 账号不存在
		if (list == null || list.isEmpty()) {
			return null;
		}
		User user = list.get(0);
		// 账号未启用、停用
		if (user.getUserStatus() == -1) {
			return null;
		}
		// 读取角色名称列表
		Set<String> roleSet = roleService.selectRoleNameSetByUserID(user.getUserID());
		// 读取应用链接列表
		Set<String> urlSet = appServiceImpl.selectAppUrlSetByUserID(user.getUserID());

		ShiroUser shiroUser = new ShiroUser(user.getUserID(), user.getUserLoginName(), user.getUserName());
		shiroUser.setRoleSet(roleSet);
		shiroUser.setUrlSet(urlSet);
		shiroUser.setOrganID(user.getOrganID());
		Organ organ = organServiceImpl.selectById(user.getOrganID());
		shiroUser.setOrganName(organ.getOrganName());
		
		SecurityUtils.getSubject().getSession().setAttribute("shiroUser", shiroUser);
		
		// 认证缓存信息
		return authc;
	}

	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
		ShiroUser shiroUser = (ShiroUser) SecurityUtils.getSubject().getSession().getAttribute("shiroUser");

		SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
		info.setRoles(shiroUser.getRoleSet());
		info.addStringPermissions(shiroUser.getUrlSet());

		return info;
	}

}
